package com.longge;

/**
 * @创建人 xinglongge
 * @创建时间 2021/10/19
 * @描述
 */
public class CompareVersion {

    public int compareVersion(String version1, String version2) {
        String[] versionOne = version1.split("\\.");
        String[] versionTwo = version2.split("\\.");
        int min = Math.min(versionOne.length, versionTwo.length);
        int i = 0;
        while (i < min) {
            if (Integer.parseInt(versionOne[i]) > Integer.parseInt(versionTwo[i])) return 1;
            if (Integer.parseInt(versionOne[i]) < Integer.parseInt(versionTwo[i])) return -1;
            i++;
        }
        //两个for循环只有一个能执行
        for (int j = i; j < versionOne.length; j++) {
            if (Integer.parseInt(versionOne[j]) != 0) return 1;
        }
        for (int j = i; j < versionTwo.length; j++) {
            if (Integer.parseInt(versionTwo[j]) != 0) return -1;
        }
        //找不到不为0的数字就返回 0
        return 0;
    }


    public static void main(String[] args) {
        int i = new CompareVersion().compareVersion("1.0.1", "1");
        System.out.println(i);
    }
}
